home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapte22 / capture.c next >
C/C++ Source or Header  |  1996-04-29  |  8KB  |  297 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "Capture.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "My Application"; 
  22.  
  23. // the rest of the stuff
  24. //......................
  25.  
  26. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  27.  
  28.  
  29. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.                       LPTSTR lpCmdLine, int nCmdShow)
  31. {
  32.    MSG      msg;
  33.    HWND     hWnd; 
  34.    WNDCLASS wc;
  35.  
  36.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  37.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  38.    wc.cbClsExtra    = 0;                      
  39.    wc.cbWndExtra    = 0;                      
  40.    wc.hInstance     = hInstance;              
  41.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  42.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  43.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  44.    wc.lpszMenuName  = lpszAppName;              
  45.    wc.lpszClassName = lpszAppName;              
  46.  
  47.    if ( IS_WIN95 )
  48.    {
  49.       if ( !RegisterWin95( &wc ) )
  50.          return( FALSE );
  51.    }
  52.    else if ( !RegisterClass( &wc ) )
  53.       return( FALSE );
  54.  
  55.    hInst = hInstance; 
  56.  
  57.    hWnd = CreateWindow( lpszAppName, 
  58.                         lpszTitle,    
  59.                         WS_OVERLAPPEDWINDOW, 
  60.                         CW_USEDEFAULT, 0, 
  61.                         CW_USEDEFAULT, 0,  
  62.                         NULL,              
  63.                         NULL,              
  64.                         hInstance,         
  65.                         NULL               
  66.                       );
  67.  
  68.    if ( !hWnd ) 
  69.       return( FALSE );
  70.  
  71.    ShowWindow( hWnd, nCmdShow ); 
  72.    UpdateWindow( hWnd );         
  73.  
  74.    while( GetMessage( &msg, NULL, 0, 0) )   
  75.    {
  76.       TranslateMessage( &msg ); 
  77.       DispatchMessage( &msg );  
  78.    }
  79.  
  80.    return( msg.wParam ); 
  81. }
  82.  
  83.  
  84. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  85. {
  86.     WNDCLASSEX wcex;
  87.  
  88.    wcex.style         = lpwc->style;
  89.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  90.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  91.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  92.    wcex.hInstance     = lpwc->hInstance;
  93.    wcex.hIcon         = lpwc->hIcon;
  94.    wcex.hCursor       = lpwc->hCursor;
  95.    wcex.hbrBackground = lpwc->hbrBackground;
  96.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  97.    wcex.lpszClassName = lpwc->lpszClassName;
  98.  
  99.    // Added elements for Windows 95.
  100.    //...............................
  101.    wcex.cbSize = sizeof(WNDCLASSEX);
  102.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  103.                             IMAGE_ICON, 16, 16,
  104.                             LR_DEFAULTCOLOR );
  105.             
  106.    return RegisterClassEx( &wcex );
  107. }
  108.  
  109.  
  110.  
  111. LRESULT CALLBACK About( HWND hDlg,           
  112.                         UINT message,        
  113.                         WPARAM wParam,       
  114.                         LPARAM lParam)
  115. {
  116.    switch (message) 
  117.    {
  118.        case WM_INITDIALOG: 
  119.                return (TRUE);
  120.  
  121.        case WM_COMMAND:                              
  122.                if (   LOWORD(wParam) == IDOK         
  123.                    || LOWORD(wParam) == IDCANCEL)    
  124.                {
  125.                        EndDialog(hDlg, TRUE);        
  126.                        return (TRUE);
  127.                }
  128.                break;
  129.    }
  130.  
  131.    return (FALSE); 
  132. }
  133.  
  134.  
  135. #define MSG_LEN          1024
  136.  
  137. char msg[MSG_LEN+1];
  138.  
  139. BOOL CALLBACK CaptureDlg(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  140.  
  141.  
  142. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  143. {
  144.    switch( uMsg )
  145.    {
  146.       case WM_COMMAND :
  147.               switch( LOWORD( wParam ) )
  148.               {
  149.                  case IDM_TEST:
  150.                         DialogBox(hInst, "CaptureDlg", hWnd, CaptureDlg);
  151.                         break;
  152.  
  153.                  case IDM_ABOUT :
  154.                         DialogBox( hInst, "AboutBox", hWnd, About );
  155.                         break;
  156.  
  157.                  case IDM_EXIT :
  158.                         DestroyWindow( hWnd );
  159.                         break;
  160.               }
  161.               break;
  162.  
  163.       case WM_DESTROY :
  164.               PostQuitMessage(0);
  165.               break;
  166.  
  167.       default :
  168.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  169.    }
  170.  
  171.    return( 0L );               
  172. }
  173.  
  174. /*****************************************************************************
  175. *  CaptureDlg
  176. *
  177. *  Creates a popup dialog which allows manipulation of the 
  178. *  master volume control.
  179. ******************************************************************************/
  180.  
  181. BOOL CALLBACK CaptureDlg(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  182. {
  183.     switch (uMsg)
  184.     {
  185.         // init dialog
  186.         //.............
  187.         
  188.         case WM_INITDIALOG:
  189.                // capture joystick
  190.                //.................
  191.  
  192.                if(joySetCapture(hDlg, JOYSTICKID1, 0, FALSE))
  193.                {
  194.                    MessageBox(hDlg, "Unable to capture the joystick", 
  195.                               NULL, MB_OK);
  196.                    EndDialog(hDlg, FALSE);
  197.                }
  198.  
  199.                // set movement threshold
  200.                //.......................
  201.                
  202.                {
  203.                   UINT nThreshold;
  204.                   
  205.                   joyGetThreshold(JOYSTICKID1, &nThreshold);
  206.                   
  207.                   if (nThreshold < 100)
  208.                   {
  209.                       nThreshold = 100;
  210.                       joySetThreshold(JOYSTICKID1, nThreshold);
  211.                   }
  212.                }
  213.                return(TRUE);
  214.  
  215.         // joystick activity
  216.         //..................
  217.  
  218.         case MM_JOY1BUTTONDOWN :
  219.         case MM_JOY1BUTTONUP :
  220.         case MM_JOY1MOVE :
  221.                {
  222.                   JOYINFO   ji;
  223.                   JOYINFOEX jiex;
  224.  
  225.                   joyGetPos(JOYSTICKID1, &ji);
  226.                   joyGetPosEx(JOYSTICKID1, & jiex);
  227.  
  228.                   // display axis positions
  229.                   //.......................
  230.  
  231.                   SetDlgItemInt(hDlg, IDC_XPOS_EC, ji.wXpos, FALSE);
  232.                   SetDlgItemInt(hDlg, IDC_YPOS_EC, ji.wYpos, FALSE);
  233.                   SetDlgItemInt(hDlg, IDC_ZPOS_EC, ji.wZpos, FALSE);
  234.  
  235.                   if (jiex.dwFlags & JOY_RETURNR)
  236.                       SetDlgItemInt(hDlg, IDC_RPOS_EC, jiex.dwRpos, FALSE);
  237.                   else
  238.                       SetDlgItemInt(hDlg, IDC_RPOS_EC, 0L, FALSE);
  239.  
  240.                   if (jiex.dwFlags & JOY_RETURNU)
  241.                       SetDlgItemInt(hDlg, IDC_UPOS_EC, jiex.dwUpos, FALSE);
  242.                   else
  243.                       SetDlgItemInt(hDlg, IDC_UPOS_EC, 0L, FALSE);
  244.  
  245.                   if (jiex.dwFlags & JOY_RETURNV)
  246.                       SetDlgItemInt(hDlg, IDC_VPOS_EC, jiex.dwVpos, FALSE);
  247.                   else
  248.                       SetDlgItemInt(hDlg, IDC_VPOS_EC, 0L, FALSE);
  249.  
  250.                   // display buttons pressed
  251.                   //........................
  252.  
  253.                   CheckDlgButton(hDlg, IDC_BTN1_CHK, (ji.wButtons & JOY_BUTTON1) );
  254.                   CheckDlgButton(hDlg, IDC_BTN2_CHK, (ji.wButtons & JOY_BUTTON2) );
  255.                   CheckDlgButton(hDlg, IDC_BTN3_CHK, (ji.wButtons & JOY_BUTTON3) );
  256.                   CheckDlgButton(hDlg, IDC_BTN4_CHK, (ji.wButtons & JOY_BUTTON4) );
  257.                }
  258.                break;
  259.  
  260.         // commands
  261.         //.........
  262.  
  263.         case WM_COMMAND:
  264.                switch ( LOWORD(wParam) )
  265.                {
  266.                   // close down
  267.                   //...........
  268.  
  269.                   case WM_CLOSE:
  270.                   case IDOK:
  271.                   case IDCANCEL:
  272.                          {
  273.                             // release joystick capture
  274.                             //.........................
  275.  
  276.                             joyReleaseCapture(JOYSTICKID1);
  277.                             
  278.                             EndDialog(hDlg, TRUE);
  279.                          }
  280.                          break;
  281.                }
  282.                break;
  283.  
  284.     }
  285.  
  286.     return (FALSE);
  287. }
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.